﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for clsSearchJob
/// </summary>
public class clsSearchJob
{
    InstantNaukriDataContextDataContext objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
    public clsSearchJob()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public clsSearchResult GetJobByJobPostId(int JobPostId)
    {
        try
        {
            clsSearchResult objclsSearchResultList = new clsSearchResult();
            objclsSearchResultList = new clsSearchResult();
            objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                      join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                      where obj.ID == JobPostId
                                      select new clsSearchResult
                                      {
                                          JOB_TITLE = obj.JOB_TITLE,
                                          SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                          EXPERIENCE_REQUIRED = obj.MIN_EXPERIENCE_REQUIRED.ToString() + " - " + obj.MAX_EXPERIENCE_REQUIRED.ToString() + " Months",
                                          JOB_LOCATION = (int)obj.JOB_LOCATION,
                                          JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                          QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                          SALARY_OFFERED = obj.SALARY_OFFERED,
                                          FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                          JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                          JOB_POST_ID = (int)obj.ID,
                                          COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                          COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                      }
                                          ).SingleOrDefault();



            return objclsSearchResultList;
        }
        catch (Exception)
        {

            throw;
        }
    }


    public List<clsSearchResult> GetJobBySkills(string[] Skills)
    {
        try
        {
            List<clsSearchResult> objclsSearchResultList = new List<clsSearchResult>();
            List<clsSearchResult> objclsSearchResultFinalList = new List<clsSearchResult>();
            for (int i = 0; i <= Skills.Length - 1; i++)
            {
                objclsSearchResultList = new List<clsSearchResult>();
                objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                          join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                          where obj.SKILLS_REQUIRED.ToLower().Contains(Skills[i])
                                          select new clsSearchResult
                                          {
                                              JOB_TITLE = obj.JOB_TITLE,
                                              SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                              EXPERIENCE_REQUIRED = obj.MIN_EXPERIENCE_REQUIRED.ToString() + " - " + obj.MAX_EXPERIENCE_REQUIRED.ToString() + " Months",
                                              JOB_LOCATION = (int)obj.JOB_LOCATION,
                                              JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                              QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                              SALARY_OFFERED = obj.SALARY_OFFERED,
                                              FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                              JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                              JOB_POST_ID = (int)obj.ID,
                                              COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                              COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                          }
                                              ).ToList();

                foreach (clsSearchResult item in objclsSearchResultList)
                {
                    objclsSearchResultFinalList.Add(item);
                }

            }
            return objclsSearchResultFinalList;
        }
        catch (Exception)
        {

            throw;
        }
    }
    public List<clsSearchResult> GetJobByIndustry(string[] Industry)
    {
        try
        {
            List<clsSearchResult> objclsSearchResultList = new List<clsSearchResult>();
            List<clsSearchResult> objclsSearchResultFinalList = new List<clsSearchResult>();
            for (int i = 0; i <= Industry.Length - 1; i++)
            {
                objclsSearchResultList = new List<clsSearchResult>();
                objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                          join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                          where objEmployer.EMPLOYER_NAME.ToLower().Contains(Industry[i])
                                          select new clsSearchResult
                                          {
                                              JOB_TITLE = obj.JOB_TITLE,
                                              SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                              EXPERIENCE_REQUIRED = obj.MIN_EXPERIENCE_REQUIRED.ToString() + " - " + obj.MAX_EXPERIENCE_REQUIRED.ToString() + " Months",
                                              JOB_LOCATION = (int)obj.JOB_LOCATION,
                                              JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                              QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                              SALARY_OFFERED = obj.SALARY_OFFERED,
                                              FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                              JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                              JOB_POST_ID = (int)obj.ID,
                                              COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                              COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                          }
                                              ).ToList();

                foreach (clsSearchResult item in objclsSearchResultList)
                {
                    objclsSearchResultFinalList.Add(item);
                }

            }
            return objclsSearchResultFinalList;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public List<clsSearchResult> GetJobByExperience(int Experience, int FunctionalAreaId)
    {
        try
        {
            List<clsSearchResult> objclsSearchResultList = new List<clsSearchResult>();

            objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                      join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                      where obj.MIN_EXPERIENCE_REQUIRED <= Experience
                                      && obj.MAX_EXPERIENCE_REQUIRED >= Experience
                                      && obj.FUNCTIONAL_AREA == FunctionalAreaId
                                      select new clsSearchResult
                                      {
                                          JOB_TITLE = obj.JOB_TITLE,
                                          SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                          EXPERIENCE_REQUIRED = obj.MIN_EXPERIENCE_REQUIRED.ToString() + " - " + obj.MAX_EXPERIENCE_REQUIRED.ToString() + " Months",
                                          JOB_LOCATION = (int)obj.JOB_LOCATION,
                                          JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                          QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                          SALARY_OFFERED = obj.SALARY_OFFERED,
                                          FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                          JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                          JOB_POST_ID = (int)obj.ID,
                                          COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                          COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                      }
                          ).ToList();


            return objclsSearchResultList;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public List<clsSearchResult> GetJobAdvanceSearch(string Skills, string Industry, int MinExp, int MaxExp, int FunctionalAreaId, int LocationId)
    {
        try
        {
            List<clsSearchResult> objclsSearchResultList = new List<clsSearchResult>();
            List<clsSearchResult> objclsSearchResultFinalList = new List<clsSearchResult>();

            string[] IndustrySet, SkillSet;


            if (Skills != "")
            {
                if (Industry != "")
                {
                    IndustrySet = Industry.Split(',');
                    for (int i = 0; i <= IndustrySet.Length - 1; i++)
                    {
                        objclsSearchResultFinalList = new List<clsSearchResult>();
                        objclsSearchResultList = new List<clsSearchResult>();
                        objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                                  join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                                  where objEmployer.EMPLOYER_NAME.ToLower().Contains(IndustrySet[i])
                                                  || obj.JOB_LOCATION == LocationId
                                                  || obj.FUNCTIONAL_AREA == FunctionalAreaId
                                                  || obj.MIN_EXPERIENCE_REQUIRED == MinExp
                                                  || obj.MAX_EXPERIENCE_REQUIRED == MaxExp
                                                  select new clsSearchResult
                                                  {
                                                      JOB_TITLE = obj.JOB_TITLE,
                                                      SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                                      EXPERIENCE_REQUIRED = Convert.ToString(obj.MIN_EXPERIENCE_REQUIRED) + " - " + Convert.ToString(obj.MAX_EXPERIENCE_REQUIRED) + " Months",
                                                      JOB_LOCATION = (int)obj.JOB_LOCATION,
                                                      JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                                      QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                                      SALARY_OFFERED = obj.SALARY_OFFERED,
                                                      FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                                      JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                                      JOB_POST_ID = (int)obj.ID,
                                                      COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                                      COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                                  }
                                                      ).ToList();

                        foreach (clsSearchResult item in objclsSearchResultList)
                        {
                            if (!objclsSearchResultFinalList.Contains(item))
                                objclsSearchResultFinalList.Add(item);
                        }

                    }
                    SkillSet = Skills.Split(',');
                    for (int i = 0; i <= SkillSet.Length - 1; i++)
                    {
                        objclsSearchResultList = new List<clsSearchResult>();
                        objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                                  join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                                  where obj.SKILLS_REQUIRED.ToLower().Contains(SkillSet[i])
                                                  || obj.JOB_LOCATION == LocationId
                                                  || obj.FUNCTIONAL_AREA == FunctionalAreaId
                                                  || obj.MIN_EXPERIENCE_REQUIRED == MinExp
                                                  || obj.MAX_EXPERIENCE_REQUIRED == MaxExp
                                                  select new clsSearchResult
                                                  {
                                                      JOB_TITLE = obj.JOB_TITLE,
                                                      SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                                      EXPERIENCE_REQUIRED = Convert.ToString(obj.MIN_EXPERIENCE_REQUIRED) + " - " + Convert.ToString(obj.MAX_EXPERIENCE_REQUIRED) + " Months",
                                                      JOB_LOCATION = (int)obj.JOB_LOCATION,
                                                      JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                                      QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                                      SALARY_OFFERED = obj.SALARY_OFFERED,
                                                      FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                                      JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                                      JOB_POST_ID = (int)obj.ID,
                                                      COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                                      COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                                  }
                                                      ).ToList();

                        foreach (clsSearchResult item in objclsSearchResultList)
                        {
                            if (!objclsSearchResultFinalList.Contains(item))
                                objclsSearchResultFinalList.Add(item);
                        }

                    }
                }
                else
                {
                    objclsSearchResultFinalList = new List<clsSearchResult>();
                    SkillSet = Skills.Split(',');
                    for (int i = 0; i <= SkillSet.Length - 1; i++)
                    {
                        objclsSearchResultList = new List<clsSearchResult>();
                        objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                                  join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                                  where obj.SKILLS_REQUIRED.ToLower().Contains(SkillSet[i])
                                                  || obj.JOB_LOCATION == LocationId
                                                  || obj.FUNCTIONAL_AREA == FunctionalAreaId
                                                  || obj.MIN_EXPERIENCE_REQUIRED == MinExp
                                                  || obj.MAX_EXPERIENCE_REQUIRED == MaxExp
                                                  select new clsSearchResult
                                                  {
                                                      JOB_TITLE = obj.JOB_TITLE,
                                                      SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                                      EXPERIENCE_REQUIRED = Convert.ToString(obj.MIN_EXPERIENCE_REQUIRED) + " - " + Convert.ToString(obj.MAX_EXPERIENCE_REQUIRED) + " Months",
                                                      JOB_LOCATION = (int)obj.JOB_LOCATION,
                                                      JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                                      QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                                      SALARY_OFFERED = obj.SALARY_OFFERED,
                                                      FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                                      JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                                      JOB_POST_ID = (int)obj.ID,
                                                      COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                                      COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                                  }
                                                      ).ToList();

                        foreach (clsSearchResult item in objclsSearchResultList)
                        {
                            if (!objclsSearchResultFinalList.Contains(item))
                                objclsSearchResultFinalList.Add(item);
                        }

                    }
                }
            }
            else
            {
                if (Industry != "")
                {
                    objclsSearchResultFinalList = new List<clsSearchResult>();
                    IndustrySet = Industry.Split(',');
                    for (int i = 0; i <= IndustrySet.Length - 1; i++)
                    {
                        objclsSearchResultList = new List<clsSearchResult>();
                        objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                                  join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                                  where objEmployer.EMPLOYER_NAME.ToLower().Contains(IndustrySet[i])
                                                  || obj.JOB_LOCATION == LocationId
                                                  || obj.FUNCTIONAL_AREA == FunctionalAreaId
                                                  || obj.MIN_EXPERIENCE_REQUIRED == MinExp
                                                  || obj.MAX_EXPERIENCE_REQUIRED == MaxExp
                                                  select new clsSearchResult
                                                  {
                                                      JOB_TITLE = obj.JOB_TITLE,
                                                      SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                                      EXPERIENCE_REQUIRED = Convert.ToString(obj.MIN_EXPERIENCE_REQUIRED) + " - " + Convert.ToString(obj.MAX_EXPERIENCE_REQUIRED) + " Months",
                                                      JOB_LOCATION = (int)obj.JOB_LOCATION,
                                                      JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                                      QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                                      SALARY_OFFERED = obj.SALARY_OFFERED,
                                                      FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                                      JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                                      JOB_POST_ID = (int)obj.ID,
                                                      COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                                      COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                                  }
                                                      ).ToList();

                        foreach (clsSearchResult item in objclsSearchResultList)
                        {
                            if (!objclsSearchResultFinalList.Contains(item))
                                objclsSearchResultFinalList.Add(item);
                        }

                    }
                }
                else
                {
                    objclsSearchResultFinalList = new List<clsSearchResult>();
                    objclsSearchResultList = new List<clsSearchResult>();
                    objclsSearchResultList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs
                                              join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.EMPLOYER_ID equals objEmployer.ID
                                              where obj.JOB_LOCATION == LocationId
                                              || obj.FUNCTIONAL_AREA == FunctionalAreaId
                                              || obj.MIN_EXPERIENCE_REQUIRED == MinExp
                                              || obj.MAX_EXPERIENCE_REQUIRED == MaxExp
                                              select new clsSearchResult
                                              {
                                                  JOB_TITLE = obj.JOB_TITLE,
                                                  SKILLS_REQUIRED = obj.SKILLS_REQUIRED,
                                                  EXPERIENCE_REQUIRED = Convert.ToString(obj.MIN_EXPERIENCE_REQUIRED) + " - " + Convert.ToString(obj.MAX_EXPERIENCE_REQUIRED) + " Months",
                                                  JOB_LOCATION = (int)obj.JOB_LOCATION,
                                                  JOB_DESCRIPTION = obj.JOB_DESCRIPTION,
                                                  QUALIFICATION_REQUIRED = obj.QUALIFICATION_REQUIRED,
                                                  SALARY_OFFERED = obj.SALARY_OFFERED,
                                                  FUNCTIONAL_AREA = (int)obj.FUNCTIONAL_AREA,
                                                  JOB_POSTED_ON = (DateTime)obj.CREATED_ON,
                                                  JOB_POST_ID = (int)obj.ID,
                                                  COMPANY_DESCRIPTION = objEmployer.COMPANY_DESCRIPTION,
                                                  COMPANY_NAME = objEmployer.EMPLOYER_NAME
                                              }
                                                  ).ToList();

                    foreach (clsSearchResult item in objclsSearchResultList)
                    {
                        if (!objclsSearchResultFinalList.Contains(item))
                            objclsSearchResultFinalList.Add(item);
                    }

                }
            }


            return objclsSearchResultFinalList;
        }
        catch (Exception)
        {

            throw;
        }
    }
}